package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysStudentExam;
import com.ruoyi.system.service.ISysStudentExamService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTestPaperMapper;
import com.ruoyi.system.domain.SysTestPaper;
import com.ruoyi.system.service.ISysTestPaperService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 考题管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-25
 */
@Service
public class SysTestPaperServiceImpl implements ISysTestPaperService 
{
    @Autowired
    private SysTestPaperMapper sysTestPaperMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysStudentExamService sysStudentExamService;

    /**
     * 查询考题管理
     * 
     * @param id 考题管理主键
     * @return 考题管理
     */
    @Override
    public SysTestPaper selectSysTestPaperById(Long id)
    {
        return sysTestPaperMapper.selectSysTestPaperById(id);
    }

    /**
     * 查询考题管理列表
     * 
     * @param sysTestPaper 考题管理
     * @return 考题管理
     */
    @Override
    public List<SysTestPaper> selectSysTestPaperList(SysTestPaper sysTestPaper)
    {
        return sysTestPaperMapper.selectSysTestPaperList(sysTestPaper);
    }

    /**
     * 新增考题管理
     * 
     * @param sysTestPaper 考题管理
     * @return 结果
     */
    @Override
    public int insertSysTestPaper(SysTestPaper sysTestPaper)
    {
        sysTestPaper.setCreateTime(DateUtils.getNowDate());
        return sysTestPaperMapper.insertSysTestPaper(sysTestPaper);
    }

    /**
     * 修改考题管理
     * 
     * @param sysTestPaper 考题管理
     * @return 结果
     */
    @Override
    public int updateSysTestPaper(SysTestPaper sysTestPaper)
    {
        this.checkStatus(sysTestPaper);
        sysTestPaper.setUpdateTime(DateUtils.getNowDate());
        return sysTestPaperMapper.updateSysTestPaper(sysTestPaper);
    }

    /**
     * 批量删除考题管理
     * 
     * @param ids 需要删除的考题管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTestPaperByIds(String ids)
    {
        return sysTestPaperMapper.deleteSysTestPaperByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除考题管理信息
     * 
     * @param id 考题管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTestPaperById(Long id)
    {
        return sysTestPaperMapper.deleteSysTestPaperById(id);
    }

    /**
     * 发布考试
     * @param id 试卷ID
     */
    @Override
    public void publish(String id) {
        // 试卷信息
        SysTestPaper sysTestPaper = this.selectSysTestPaperById(Long.valueOf(id));
        // 查看考试信息是否已经发过了
        this.checkStatus(sysTestPaper);
        // 查询对应考试班级下的学生信息
        List<SysUser> sysUsers = this.findUserByDept(sysTestPaper.getDeptId());
        // 构造考试信息
        List<SysStudentExam> sysStudentExams = this.buildSysStudentExamList(sysTestPaper, sysUsers);
        // 持久化数据
        sysStudentExams.parallelStream().forEach(sysStudentExam -> {
            sysStudentExamService.insertSysStudentExam(sysStudentExam);
        });
    }

    public void checkStatus(SysTestPaper sysTestPaper) {
        SysStudentExam sysStudentExam = new SysStudentExam();
        sysStudentExam.setTestPaperId(sysTestPaper.getId());
        List<SysStudentExam> sysStudentExams = sysStudentExamService.selectSysStudentExamList(sysStudentExam);
        if (!CollectionUtils.isEmpty(sysStudentExams)) {
            throw new RuntimeException("考试信息已经发布过了");
        }
    }

    public List<SysStudentExam> buildSysStudentExamList(SysTestPaper sysTestPaper, List<SysUser> sysUsers) {
        return sysUsers.stream().map(sysUser -> {
            SysStudentExam sysStudentExam = new SysStudentExam();
            // 设置试卷相关属性
            sysStudentExam.setTestPaperId(sysTestPaper.getId());
            sysStudentExam.setTitle(sysTestPaper.getTitle());
            sysStudentExam.setCourseId(sysTestPaper.getCourseId());
            sysStudentExam.setCourseName(sysTestPaper.getCourseName());
            sysStudentExam.setDeptId(sysTestPaper.getDeptId());
            sysStudentExam.setDeptName(sysTestPaper.getDeptName());
            // 设置考试时间相关属性
            sysStudentExam.setDuration(sysTestPaper.getDuration());
            sysStudentExam.setGrade(sysTestPaper.getGrade());
            // 设置考生信息
            sysStudentExam.setUserId(sysUser.getUserId());
            sysStudentExam.setUsername(sysUser.getUserName());
            // 初始化考试状态和分数
            sysStudentExam.setScore(0L);  // 初始分数为0
            sysStudentExam.setStatus("WAITING_FOR_THE_EXAM");  // 初始状态为"未开始"

            return sysStudentExam;
        }).collect(Collectors.toList());
    }


    /**
     * 感觉部门ID 查询该部门下的学生
     * @param deptId 部门ID
     * @return 该部门下成员
     */
    public List<SysUser> findUserByDept(Long deptId) {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(deptId);
        List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
        if (CollectionUtils.isEmpty(sysUsers)) {
            throw new RuntimeException("该班级下没有学生信息，无法发起考试");
        }
        return sysUsers;
    }
}
